package control;

import java.awt.BorderLayout;
import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.Timer;

import model.Element;
import model.Player;
import model.World;
import model.element.Building;
import model.element.ElementFactory;
import model.element.Road;
import model.element.Unit;
import view.BuildMenu;
import view.Map;
import view.View;

public class Game implements ActionListener{
	private Timer timer = new Timer(20, this);
	private World world = new World();
	private View view;
	private Map map;
	private BuildMenu menu;
	private Player player;
	
	public Game(){
		player = new Player();
		view = new View("Hell Tycoon");
		view.addCenter(map = new Map(), BorderLayout.CENTER);
		KeyboardFocusManager.getCurrentKeyboardFocusManager()
		  .addKeyEventDispatcher(new KeyEventDispatcher() {
		      @Override
		      public boolean dispatchKeyEvent(KeyEvent e) {
		    	  map.movement(e.getKeyChar());
		    	  return false;
		      }
		});
		MapMouseListener listener = new MapMouseListener(player, this, map, world);
		map.addMouseListener(listener);
		map.addMouseMotionListener(listener);
		view.addCenter(menu = new BuildMenu(listener), BorderLayout.SOUTH);
		/* test stuff */
		menu.addOption(new Road(0,0));
		menu.addOption(new Building(0,0,60,60));
		makeTestWorld();
	}
	
	public void makeTestWorld(){
		ElementFactory f = new ElementFactory();
		Element road = f.createRoad(0, 0, world);
		addElement(road);
		addElement(f.createRoad(20, 0, world));
		addElement(f.createRoad(40, 0, world));

		Element goal = f.createRoad(100, 0, world);
		addElement(goal);
		addElement(f.createRoad(200, 200, world));
		

		makeRoad(0, 20, 0, 200);
		makeRoad(20, 40, 180, 480);
		makeRoad(20, 220, 20, 40);
		makeRoad(200, 220, 40, 360);
		makeRoad(200, 220, 40, 360);
		makeRoad(20, 200, 100, 120);
		makeRoad(40, 200, 180, 200);

		Element goal3 = f.createRoad(220, 200, world);
		Element goal2 = f.createRoad(60, 120, world);

		

		addElement(goal2);
		addElement(goal3);
		Unit unit = new Unit(0,0,8,8,(Road)road);


		unit.search((Road)goal3, world);

		//unit.search((Road)goal, world);

		addElement(unit);
		
	}
	
	public void makeRoad(int xStart, int xStop, int yStart, int yStop){
		int xStartTemp = xStart;
		int yStartTemp = yStart;
		ElementFactory f = new ElementFactory();
		for(xStart = xStartTemp; xStart < xStop; xStart += Road.ROAD_SIZE){
			for(yStart = yStartTemp; yStart < yStop; yStart += Road.ROAD_SIZE){
				addElement(f.createRoad(xStart, yStart, world));
			}
		}
	}
	
	public void addElement(Element e){
		if(world.add(e)){
			if(e instanceof Road){
				world.addRoad((Road)e);
			}
			map.add(e);
		}
	}
	
	public void start(){
		timer.start();
	}
	
	public void stop(){
		timer.stop();
	}
	
	public void toggle(){
		if(timer.isRunning()){
			stop();
		}else{
			start();
		}
	}

	public void actionPerformed(ActionEvent event) {
		world.tick();
		map.tick();
	}

	public static void main(String[] e){
		Game g = new Game();
		g.start();
	}
	 
}
